<?php

	include ("../includes/connect_db.php");
	include("../includes/core.php");

	$act = $_POST['act'];
	$period = $_POST['period'];
	$date = $_POST['date'];
	$agent = $_POST['agent'];
	$status = $_POST['status'];

	$foEdit = $_POST['foEdit'];
	$agentEdit = $_POST['agentEdit'];
	$statusEdit = $_POST['statusEdit'];

	switch ($act) {
		case 'view':
			$sqlAgent = "SELECT DATE_FORMAT(b.checkout, '%M %d, %Y') AS 'date_tampil', a.id_agent, 
			c.nama AS agent_name, b.nama AS customer_name, a.comission AS agent_comission, a.id_fo AS id_fo,
			b.idroom AS id_room, a.is_paid AS comission_status, d.nama AS room_name FROM agent_paid a 
			LEFT JOIN front_office b ON b.id = a.id_fo AND b.idagent = a.id_agent
			LEFT JOIN agent c ON c.id = a.id_agent
			LEFT JOIN room d ON d.id = b.idroom
			WHERE b.is_paid = 1";
			echo $sqlAgent;
			if ($agent != '*') {
				$sqlAgent .= " AND a.id_agent = '".$agent."'";
			}
			if ($status != '*') {
				$sqlAgent .= " AND a.is_paid = '".$status."'";
			}
			if ($period != '*') {
				if ($period == 'dayviewagent') {
					$sqlAgent .= " AND DATE(b.checkout) = '".$date."'";
				}elseif ($period == 'monthviewagent') {
					$sqlAgent .= " AND CONCAT(YEAR(b.checkout),'-',MONTH(b.checkout)) = '".$date."'";
				}elseif ($period == 'yearviewagent') {
					$sqlAgent .= " AND YEAR(b.checkout) = '".$date."'";
				}
			}

			$sqlAgent .= " ORDER BY (b.checkout) ASC";

			$queryAgent = mysql_query($sqlAgent);
			if (mysql_num_rows($queryAgent) > 0) {
				$i = 1;
				while ($barisAgent = mysql_fetch_array($queryAgent)) {
					?>
					<tr>
						<td class="al_center" valign="middle">
							<?php echo $i ?>
							<input type="hidden" class="id-agent-edit" value="<?php echo $barisAgent['id_agent'] ?>">
							<input type="hidden" class="id-fo-edit" value="<?php echo $barisAgent['id_fo'] ?>">
						</td>
						<td class="al_center" valign="middle"><?php echo $barisAgent[agent_name] ?></td>
						<td class="al_center" valign="middle"><?php echo $barisAgent[customer_name] ?></td>
						<td class="al_center" valign="middle"><?php echo $barisAgent[room_name] ?></td>
						<td class="al_center" valign="middle"><?php echo $barisAgent[date_tampil] ?></td>
						<td class="al_center currency" valign="middle"><?php echo $barisAgent[agent_comission] ?></td>
						<td class="al_center" valign="middle">
							<div class="make-switch status-bayar" data-on-label="Paid" data-off-label="Unpaid">
								<?php  
									if ($barisAgent['comission_status'] == 1) {
										$checked = "checked";
									} else {
										$checked = "";
									}
								?>
								<input name="status" class="cek-status" type="checkbox" <?php echo $checked; ?>>
							</div>
							<div class="undo-form">
								<h5><i class="fa fa-check-square-o"></i> Paid | <a href="" class="undo-paid"><i class="fa fa-undo"></i> Undo</a></h5>
							</div>
						</td>
					</tr>
					<?php
					$i++;
				}
			}else{
				?>
				<tr>
					<td colspan="7">No result has shown.</td>
				</tr>
				<?php
			}
			?>
			<script type="text/javascript">
				$('.make-switch').bootstrapSwitch();
				$('.undo-form').hide();
			</script>

			<?php
			break;

		case 'edit':
			$sqlSelect = "SELECT * FROM agent WHERE id = '".$agentEdit."'";
			$querySelect = mysql_query($sqlSelect);
			$namaAgent = mysql_fetch_array($querySelect);

			if ($statusEdit == 1) {
				$code = "PAID";
			}else{
				$code = "UNPAID";
			}

			$sql = "UPDATE agent_paid SET is_paid = '".$statusEdit."'
			WHERE id_fo = '".$foEdit."' AND id_agent = '".$agentEdit."'";
			if ($query = mysql_query($sql)) {
				echo $code;
			}else{
				echo "Payment agent status is not successfully changed.";
			}
			break;
	}
?>